package controller

import (
	"data-system/config"
	"data-system/models"
	"fmt"
	"log"
	"path/filepath"
	"strings"

	"github.com/gin-gonic/gin"
	"github.com/samber/lo"

	"io/fs"
)

type Assets struct {
	SequenceFrames []string `json:"sequenceFrames"`
	Spines         []string `json:"spines"`
}
type AssetReq struct {
	SpineDirs         []string `json:"spineDirs"`
	SequenceFrameDirs []string `json:"sequenceFrameDirs"`
	SequenceFrameKeys []string `json:"sequenceFrameKeys"`
}

func GetAssets(c *gin.Context) {
	var req AssetReq
	if err := c.Bind(&req); err != nil {
		log.Println(fmt.Errorf("参数解析异常,%v", err))
		c.JSON(models.FailData(err.Error()))
		return
	}
	assets, err := readAssets(req, config.Cfg().AssetDir)
	if err != nil {
		log.Println(err)
		c.JSON(models.FailData(err.Error()))
		return
	}
	c.JSON(models.OkData(assets))
}

func readAssets(req AssetReq, assetDir string) (Assets, error) {
	var assets Assets
	var uris []string
	err := filepath.WalkDir(assetDir, func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return fmt.Errorf("访问路径 %s 时出错: %w", path, err)
		}
		if !d.IsDir() {
			uris = append(uris, path)
		}
		return nil
	})
	if err != nil {
		return assets, fmt.Errorf("遍历目录 %s 失败: %w", assetDir, err)
	}

	var spines = make([]string, 0)
	var sequenceFrames = make([]string, 0)
	for _, uri := range uris {
		_, isSeFr := lo.Find(req.SequenceFrameDirs, func(sequenceFrameDir string) bool {
			exist := strings.HasPrefix(uri, assetDir+sequenceFrameDir)
			return exist
		})
		if isSeFr {
			split := strings.Split(uri, "\\")
			name := split[len(split)-2]
			_, isLastDir := lo.Find(req.SequenceFrameKeys, func(sequenceFrameKey string) bool {
				exist := strings.HasSuffix(uri, sequenceFrameKey)
				return exist
			})
			if isLastDir {
				name = split[len(split)-3]
			}
			sequenceFrames = append(sequenceFrames, name)
		} else {
			_, isSpine := lo.Find(req.SpineDirs, func(spineDir string) bool {
				exist := strings.HasPrefix(uri, assetDir+spineDir)
				return exist
			})
			if isSpine {
				split := strings.Split(uri, "\\")
				name := split[len(split)-2]
				spines = append(spines, name)
			}
		}
	}
	assets = Assets{SequenceFrames: sequenceFrames, Spines: spines}
	return assets, nil
}

//func readAssets(req AssetReq, rootAssetDir string) (Assets, error) {
//	var assets Assets
//	uris, err := GetDirAllAssets(rootAssetDir)
//	if err != nil {
//		return assets, err
//	}
//	var excludes = make([]string, 0)
//	var sequenceFrames = make([]string, 0)
//	var spines = make([]string, 0)
//	var pngs = make([]string, 0)
//	for _, uri := range uris {
//
//		if strings.HasSuffix(uri, ".skel") && strings.HasSuffix(uri, ".atlas") {
//			split := strings.Split(uri, "\\")
//			name := split[len(split)-3]
//			spines = append(spines, name)
//			dirPath := strings.Replace(uri, split[len(split)-1], "", 1)
//			excludes = append(excludes, dirPath)
//		} else if strings.HasSuffix(uri, ".atlas") {
//			split := strings.Split(uri, "\\")
//			name := split[len(split)-2]
//			sequenceFrames = append(sequenceFrames, name)
//			dirPath := strings.Replace(uri, split[len(split)-1], "", 1)
//			excludes = append(excludes, dirPath)
//		}
//	}
//	for _, uri := range uris {
//		if !strings.HasSuffix(uri, ".png") {
//			continue
//		}
//		var exist = false
//		for _, prefix := range excludes {
//			if strings.HasPrefix(uri, prefix) {
//				exist = true
//				break
//			}
//		}
//		if !exist {
//			split := strings.Split(uri, "\\")
//			name := split[len(split)-1]
//			pngs = append(pngs, name)
//		}
//	}
//	assets = Assets{SequenceFrames: sequenceFrames, Spines: spines, Pngs: pngs}
//	return assets, nil
//}
